<?php

Raise::load('core.ui.RaiseUIControl');
Raise::load('core.collection.RaiseCollection');
Raise::load('core.reflection.RaiseVariable');
Raise::load('core.js.RaiseJavascript');

/**
 * GoogleAnalytics UIControl
 * Google Analytics Javascript snippet for tracking
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package phpraise.control
 * @since 1.2
 */
class GoogleAnalytics extends RaiseUIControl {

    /**
     * Unique ID of the tracking profile
     * @var string
     */
    private $code;

    /**
     * A collection of all the parameters for Google Analytics
     * @var RaiseCollection
     */
    private $params;

    /**
     * Create a new Google Analytics instance
     * @param string $code The unique ID of the tracking profile (aka. Web Property ID)
     */
    public function __construct($code) {
        $this->code($code);
        $this->params = new RaiseCollection();
    }

    /**
     * Get or set the unique ID of the tracking profile (aka. Web Property ID)
     * @param string $id
     * @return string
     */
    public function code($id = false){
        if($id !== false){
            $this->code = $id;
        }
        return $this->code;
    }

    /**
     * Get or set the collection of parameters for the tracking code
     * @param RaiseCollection $t
     * @return RaiseCollection
     */
    public function params($t = false){
        if(func_num_args () == 1){
            $this->params = $t;
        }
        return $this->params;
    }

    /**
     * Prebuild and prepare the parameters for use in output
     * @return string
     */
    private function prebuild(){
        $this->params()->add('_setAccount', $this->code);
        $this->params()->add('_trackPageview');
        $i = $this->params()->iterator();
        $buffer = '';
        while($a = $i->iterate()){
            if(is_string($a->key())){
                $buffer .= '_gaq.push([\''.addslashes($a->key()).'\',\''.addslashes($a->value()).'\'])';
            }else{
                if(RaiseVariable::isType($a->value(), RaiseVariable::TYPEARRAY) || $a->value() instanceof RaiseCollection){
                    $b = $a['value'];
                    foreach($b as &$c){
                        if(is_string($c)){
                            $c = '\''.addslashes($c).'\'';
                        }
                    }
                    $buffer .= '_gaq.push([';
                    $buffer .= RaiseText::listing($b, ',');
                    $buffer .= '])';
                }else{
                    $buffer .= '_gaq.push([\''.addslashes($a->value()).'\'])';
                }
            }
        }
        return $buffer;
    }

    public function output() {
        return RaiseJavascript::block('var _gaq = _gaq || [];' . $this->prebuild() . '

  (function() {
    var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;
    ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';
    var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);
  })();');
    }

    

}
